//
//  StoreRecommendScreeningView.m
//  BaseFrame
//
//  Created by 吕金状 on 2017/8/10.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "StoreRecommendScreeningView.h"

@interface StoreRecommendScreeningView ()

Copy void(^selectedHandle)(NSString *str);
Strong UIView *bgView;

@end

@implementation StoreRecommendScreeningView

- (instancetype)initWithFrame:(CGRect)frame selectedHandle:(void (^)(NSString *))selectedHandle{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        _selectedHandle = selectedHandle;
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
//        backButton.backgroundColor = [UIColor blackColor];
        backButton.alpha = .6;
        backButton.titleLabel.font = DEF_FontSize_13;
        [backButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        _bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        [self addSubview:_bgView];
        self.bgView.backgroundColor = getColor(@"ffffff");
    }
    return self;
}
//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
-(void)dismiss{
    if (_getDisMissClick) {
        _getDisMissClick();
    }
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    self.bgView.frame = CGRectMake(0, self.offsetY + 64, SCREEN_WIDTH, 38 / WIDTH_5S_SCALE* (dataArray.count / 4 + (dataArray.count % 4 == 0 ? 0 : 1)));
    for (int i = 0; i < dataArray.count; i ++) {
        UIButton *typeButton = [[UIButton alloc]initWithFrame:CGRectMake((i % 4) * SCREEN_WIDTH / 4 + (SCREEN_WIDTH / 4 - 64 / WIDTH_5S_SCALE) / 2, (38  * (i / 4) + (38 - 24 ) / 2 ) / WIDTH_5S_SCALE, 64 / WIDTH_5S_SCALE, 24 / WIDTH_5S_SCALE)];
        typeButton.titleLabel.font = DEF_FontSize_14;
        [typeButton setTitle:dataArray[i][@"rc_name"] forState:UIControlStateNormal];
        [typeButton setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        [typeButton addTarget:self action:@selector(typeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        typeButton.backgroundColor = getColor(@"eeeeee");
        typeButton.layer.masksToBounds = YES;
        typeButton.layer.cornerRadius = 12 / WIDTH_5S_SCALE;
        typeButton.tag = 2000 + i;
        if (i == self.chooseIndex) {
            typeButton.backgroundColor = getColor(mainColor);
            [typeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        [self.bgView addSubview:typeButton];
        
    }
}

- (void)typeButtonClick:(UIButton *)sender{
    [self dismiss];
    NSString *str = [NSString stringWithFormat:@"%ld",sender.tag - 2000];
    if (self.selectedHandle) {
        self.selectedHandle(str);
    }
}

@end
